package org.xiaojl.modules.sys.controller;

import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.xiaojl.common.base.BaseController;
import org.xiaojl.dwz.DwzAjax;
import org.xiaojl.dwz.PageBean;
import org.xiaojl.dwz.PageParam;
import org.xiaojl.modules.sys.entity.SysModule;
import org.xiaojl.modules.sys.entity.SysResource;
import org.xiaojl.modules.sys.service.SysModuleService;
import org.xiaojl.modules.sys.service.SysResourceService;
import org.xiaojl.utils.ToolUtil;

import com.baomidou.mybatisplus.mapper.EntityWrapper;

/**
 * <p>
 * 系统模块表 前端控制器
 * </p>
 *
 * @author xiaojl
 * @since 2017-08-31
 */
@Controller
@RequestMapping("/sys/sysModule")
public class SysModuleController extends BaseController {
	private static final Logger log = LoggerFactory.getLogger(SysModuleController.class);
	private static final String basePagePath = "sys/sysModule/";

	@Autowired
	private SysModuleService sysModuleService;
	@Autowired
	private SysResourceService sysResourceService;
	
	@RequiresPermissions(basePagePath+"list")
	@RequestMapping("/list")
	public ModelAndView list(PageParam pageParam, SysModule entity, Model model){
		try {
			PageBean<SysModule> pageBean = this.selectListPage(pageParam, entity, sysModuleService);
			
			model.addAttribute("pageBean", pageBean);
			model.addAttribute("pageParam", pageParam);
			model.addAttribute("entity", entity);
			
			return operatePage(basePagePath+"list",model);
		} catch (Exception e) {
			log.error("查询列表数据出现异常:", e);
			return operateError("获取数据失败");
		}
	}
	
	/**
	 * 转到添加页面 .
	 */
	@RequiresPermissions(basePagePath+"add")
	@RequestMapping("/addUI")
	public ModelAndView addUI(Model model) {
		try {
			int count = sysModuleService.selectCount(new EntityWrapper<SysModule>().eq("status",0));
			model.addAttribute("seq", count+1);
			return operatePage(basePagePath+"add",model);
			
		} catch (Exception e) {
			log.error("进入添加页面出现异常:", e);
			return operateError("获取数据失败");
		}
	}
	
	/**
	 * 保存 .
	 */
	@RequiresPermissions(basePagePath+"add")
	@RequestMapping("/add")
	public ModelAndView add(Model model, SysModule entity, DwzAjax dwz) {
		try {
			List<SysModule> listByName = sysModuleService.selectList(new EntityWrapper<SysModule>().eq("name",entity.getName()));
			if (ToolUtil.isNotEmpty(listByName)) {
				return operateError("模块名称【" + entity.getName() + "】已存在");
			}
			
			sysModuleService.insert(entity);
			return operateSuccess(dwz);
		} catch (Exception e) {
			log.error("添加数据出现异常:", e);
			return operateError("保存数据失败");
		}
	}
	
	/**
	 * 转到修改页面 .
	 */
	@RequiresPermissions(basePagePath+"edit")
	@RequestMapping("/editUI")
	public ModelAndView editUI(Model model, Long id) {
		try {
			SysModule entity = sysModuleService.selectById(id);
			if (ToolUtil.isEmpty(entity)) {
				return operateError("获取数据失败");
			}

			model.addAttribute("entity",entity);
			return operatePage(basePagePath+"edit",model);
		} catch (Exception e) {
			log.error("进入编辑页面时，获取数据出现异常:id="+id, e);
			return operateError("获取数据失败");
		}
	}
	
	/**
	 * 保存修改后的信息 .
	 * 
	 */
	@RequiresPermissions(basePagePath+"edit")
	@RequestMapping("/edit")
	public ModelAndView edit(SysModule entity, DwzAjax dwz) {
		try {
			Long id = entity.getId();

			SysModule oldEntity = sysModuleService.selectById(id);
			if (ToolUtil.isEmpty(oldEntity)) {
				return operateError("无法获取要修改的数据");
			}
			
			if(!entity.getName().equals(oldEntity.getName())){
				List<SysModule> listByName = sysModuleService.selectList(new EntityWrapper<SysModule>().eq("name",entity.getName()));
				if (ToolUtil.isNotEmpty(listByName)) {
					return operateError("模块名称【" + entity.getName() + "】已存在");
				}
			}
			
			sysModuleService.updateById(entity);
			return operateSuccess(dwz);
		} catch (Exception e) {
			log.error("保存修改数据出现异常:", e);
			return operateError("保存失败");
		}
	}
	
	
	/**
	 * 删除
	 */
	@RequiresPermissions(basePagePath+"delete")
	@RequestMapping("/delete")
	public ModelAndView delete(Long id, DwzAjax dwz) {
		try {
			SysModule entity = sysModuleService.selectById(id);
			if (ToolUtil.isEmpty(entity)) {
				return operateError("无法获取要删除的数据");
			}
			
			List<SysResource> list = sysResourceService.selectList(new EntityWrapper<SysResource>().eq("pid",0));
			if(list!=null && list.size()>0){
				return operateError("该模块下存在功能菜单，请先取消对应功能菜单关联！");
			}
			
			sysModuleService.deleteById(id);
			return operateSuccess(dwz);
		} catch (Exception e) {
			log.error("删除出现异常:id="+id, e);
			return operateError("删除失败");
		}
	}
}
